<?php
/**
 * Extend Zend_Application_Resource_Translate
 * Auto select translate file by locale
 * 
 * @copyright  Copyright (c) 2011 Ricky Feng (http://code.google.com/p/rphp4zf)
 * @license    New BSD License
 */

class Mediad2_Application_Resource_Acl extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
    	$options = $this->getOptions();
    	
    	//判断是否设置options中的registryName，如果没有则增加默认值
    	if (!isset($options['registryName'])) {
    		$options['registryName'] = 'acl';
    	}
    	
		//初始化acl
		$acl = new Zend_Acl();
		
		//增加资源
		$servicePermission = new Mediad2_Service_Admin_Permission();
		$resources = $servicePermission->fetchAclResource(null,null,null,null,false);
		foreach ($resources as $key => $value) {
			$acl->addResource($value);
		}
		
		//增加角色
		$serviceRole = new Mediad2_Service_Admin_Role();
		$roles = $serviceRole->fetchAclRole(null,null,null,null,false);
		foreach ($roles as $key => $value) {
			$acl->addRole($value['code'], (empty($value['parents'])?null:explode(',', $value['parents'])));
		}
		
		//增加规则
		$serviceRule = new Mediad2_Service_Admin_RolePermission();
		$rules = $serviceRule->fetchAll(null,null,null,null,false);
		foreach ($rules as $key => $value) {
			$acl->allow($value->getRole(),$value->getResource(),$value->getPrivilege());
		}
		
		//保存进注册表
		Zend_Registry::set($options['registryName'], $acl);
		
		return $acl;

    }
}

